﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.SysParam;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;
namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SysParamController : AdminController
    {
        [Import]
        private ISysParamService SysParamService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new SysParamModel();
            return View(model);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = SysParamService.SysParams.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = SysParamService.SysParams.Where(expr).Select(t => new SysParamModel
            {
                Id = t.Id,
                OrderNo = t.OrderNo,
                Shi_SXS=t.Shi_SXS,
                Yong_LQZ=t.Yong_LQZ,
                Yong_LXS=t.Yong_LXS,
                Dan_JXS=t.Dan_JXS,
                Fei_YQZ=t.Fei_YQZ,
                Fei_YXS=t.Fei_YXS,
                Fu_YLBJ=t.Fu_YLBJ,
                Fu_YLGZ=t.Fu_YLGZ,
                Huan_BZQ=t.Huan_BZQ,
                Huan_BTX=t.Huan_BTX,
                Qian_FHB=t.Qian_FHB,
                Qian_FBG=t.Qian_FBG

            }).OrderBy(t => t.OrderNo).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Shi_SXS.ToString(),
                            FormatQZ(c.Yong_LQZ),
                            c.Yong_LXS.ToString(),
                            c.Dan_JXS.ToString(),
                            FormatQZ(c.Fei_YQZ),
                            c.Fei_YXS.ToString(),
                            ParseFuYLBJ(c.Fu_YLBJ),
                            ParseFuYLGZ(c.Fu_YLGZ),
                            c.Huan_BZQ.ToString(),
                            c.Huan_BTX.ToString(),
                            ParseQianFHB(c.Qian_FHB),
                            ParseQianFHB(c.Qian_FBG),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public string FormatQZ(byte yong_lqz)
        {
            string result = "";
            switch (yong_lqz)
            {
                case 0:
                    result = "四舍五入";
                    break;
                case 1:
                    result = "进位";
                    break;
                case 2:
                    result = "舍去";
                    break;
                default:
                    result = "错误";
                    break;
            }
            return result;
        }

        private string ParseFuYLBJ(byte tag)
        {
            string name = "";
            switch(tag)
            {
                case 0:
                    name = "不启用";
                    break;
                case 1:
                    name = "启用";
                    break;
                default:
                    name = "";
                    break;
            }
            return name;
        }

        private string ParseFuYLGZ(byte tag)
        {
            string name = "";
            switch (tag)
            {
                case 0:
                    name = "由操作员人工处理";
                    break;
                case 1:
                    name = "用量按零处理";
                    break;
                case 2:
                    name = "用量按实际值处理";
                    break;
                default:
                    name = "";
                    break;
            }
            return name;
        }

        private string ParseQianFHB(byte tag)
        {
            string name = "";
            switch (tag)
            {
                case 0:
                    name = "允许";
                    break;
                case 1:
                    name = "不允许";
                    break;
                default:
                    name = "";
                    break;
            }
            return name;
        }

        //public ActionResult Create()
        //{
        //    var model = new SysParamModel();

        //    return PartialView(model);
        //}

        //[HttpPost]
        //[AdminOperateLog]
        //public ActionResult Create(SysParamModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        OperationResult result = SysParamService.Insert(model);
        //        if (result.ResultType == OperationResultType.Success)
        //        {
        //            return Json(result);
        //        }
        //        else
        //        {
        //            return PartialView(model);
        //        }
        //    }
        //    else
        //    {
        //        return PartialView(model);
        //    }
        //}

        public ActionResult Edit(int Id)
        {
            var model = new SysParamModel();
            var entity = SysParamService.SysParams.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new SysParamModel
                {

                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(SysParamModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = SysParamService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = SysParamService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<SysParam, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<SysParam> bulider = new DynamicLambda<SysParam>();
            Expression<Func<SysParam, Boolean>> expr = null;

            Expression<Func<SysParam, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }


    }
}
